package com.wdc.heap;

import com.wdc.queue.Arr;

import java.util.Arrays;

//建堆   大顶堆
@SuppressWarnings("all")
public class MaxHeap {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7};
        MaxHeap maxHeap = new MaxHeap(array);
        System.out.println(Arrays.toString(maxHeap.array));

        while (maxHeap.size > 1){
            maxHeap.swap(0, maxHeap.size - 1);
            maxHeap.size--;
            maxHeap.down(0);
        }
        System.out.println(Arrays.toString(maxHeap.array));
    }
    int[] array;
    int size;
    public MaxHeap(int capacity){this.array = new int[capacity];}

    /**
     * 获取堆顶元素
     * @return
     */
    public int peek(){
        return array[0];
    }

    public boolean isEmty(){
        return size == 0;
    }

    public boolean isFull(){
        return size == array.length;
    }


    /**
     * 删除堆顶元素
     */
    public int poll(){
        int top = array[0];
        swap(0,size - 1);
        size--;
        down(0);

        return top;
    }


    /**
     * 删除指定索引位置处的元素
     * @param index 索 引
     * @return  被删除元素
     */
    public int poll(int index){
        int deleted = array[index];
        swap(index,size - 1);
        index--;
        down(index);
        return deleted;
    }

    /**
     * 替换堆顶元素
     * @param replaced 新元素
     */
    public void repalce(int replaced){
        array[0] = replaced;
        down(0);
    }

    /**
     * 向堆的尾部添加元素
     * @param offered 新元素
     * @return
     */
    public boolean offer(int offered){
        if (isFull()){
            return false;
        }
        //array[size++] = offered;
        up(offered);
        size++;
        return true;
    }

    /**
     * 将 offered 元素上浮：直到 offered 小于父元素 或者到堆顶
     * @param offered
     */
    private void up(int offered){
       int child = size;
       while(child > 0){
           //找到父元素
           int parent = (child -1) / 2;
           if (offered > array[parent] ){
//               swap(child,parent);
               array[child] = array[parent];
           }else {
               break;
           }
           child = parent;

       }
       array[child] = offered;
    }



    public MaxHeap(int[] array){
        //传进来的普通数组与成员方法关联
        this.array = array;
        this.size = array.length;
        //然后进行建堆
        heapify();
    }

    //建堆
    private void heapify(){
        //最后非叶子节点
        for (int i = size / 2 -1; i >= 0 ; i--) {
            down(i);
        }
    }

    //将  parent 索引处的元素下潜：与两个孩子较大者交换，直到没孩子 或者没有孩子比它大
    private void down(int parent) {
        //找到他的孩子
        int left = parent * 2 + 1;
        int right = left + 1;
        int max = parent;
        if (left < size && array[left] > array[max]){
            max = left;
        }
        if (right < size && array[right] > array[max]){
            max = right;
        }
        if (max != parent){
            swap(max,parent);
            down(max);
        }

    }

    private void swap(int i,int j){
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

}
